package  
{
	import org.flixel.FlxPoint;
	import pessoas.*;
	/**
	 * ...
	 * @author João Brant
	 */
	public class EntityFactory 
	{
		public var createPedestreTimer:Number = 5;
		private var createPedestreInterval:Number = 2;
		
		public var createNonPedestreTimer:Number = 0;
		private var createNonPedestreInterval:Number = 4;
		private const createNonPedestreIntervalMinimum:int = 3;
		
		private var mainState:MainState;
		
		private var pedestreMinY:int = 10;
		private var pedestreAreaHeight:int = - 64;
		
		private var nonPedestreMinY:int = 10;
		private var nonPedestreAreaHeight:int = - 64;
		
		private var spawnPosLeft:int =  -64;
		private var spawnPosRight:int = 64;
		
		public function EntityFactory(_mainState:MainState)
		{
			mainState = _mainState;
		}
		
		public function update():void
		{
			createPedestreTimer += Main.delta * (0.5 * Math.random() + 0.75);
			createNonPedestreTimer += Main.delta * (0.5 * Math.random() + 0.75);
			
			if (!mainState.screenStop)
			{
				createNonPedestreInterval = 0.333 + mainState.timeLeft / 15;
				createPedestreInterval = 0.333 + mainState.timeLeft / 30;
			}
			else
			{
				createNonPedestreInterval = 1 + mainState.timeLeftBefore / 15;
				createPedestreInterval = 1 + mainState.timeLeftBefore / 30;
			}
			
			if (createNonPedestreInterval < createNonPedestreIntervalMinimum)
				createNonPedestreInterval = createNonPedestreIntervalMinimum;
			
			var pos:FlxPoint = new FlxPoint();
			var dir:int;
			
			var numberOfPeople:int, i:int;
			
			//Cria policiais e midia
			if (createNonPedestreTimer > createNonPedestreInterval)
			{
				//É quadrática a distribuição pra ficar muito foda de vir quatro manos
				numberOfPeople = 1 + Math.random() * Math.random() * PlayerData.level;
				
				for (i = 0; i < numberOfPeople; i++)
				{
					if (Math.random() < 0.5)
					{
						pos.x = spawnPosLeft + mainState.linha4.x;
						dir = 1;
					}
					else
					{
						pos.x = spawnPosRight + mainState.linha5.x;
						dir = -1;
					}
					
					createNonPedestreTimer -= createNonPedestreInterval;
					
					pos.y = nonPedestreMinY + mainState.linha1.y + Math.random() * (nonPedestreAreaHeight + mainState.linha3.y - (nonPedestreMinY + mainState.linha1.y));
					
					if (Math.random() < 0.7)
					{	
						var p:Policial = mainState.pessoasGroup.recycle(Policial) as Policial;
						p.respawn(mainState, pos, dir);
					}
					else
					{
						var m:Midia = mainState.pessoasGroup.recycle(Midia) as Midia;
						m.respawn(mainState, pos, dir);
					}
				}
			}
			
			//Cria ricos e pobres
			if (createPedestreTimer > createPedestreInterval)
			{
				//É quadrática a distribuição pra ficar muito foda de vir quatro manos
				numberOfPeople = 1 + Math.random() * Math.random() * PlayerData.level;
				
				for (i = 0; i < numberOfPeople; i++)
				{
					if (Math.random() < 0.5)
					{
						pos.x = spawnPosLeft + mainState.linha4.x;
						dir = 1;
					}
					else
					{
						pos.x = spawnPosRight + mainState.linha5.x;
						dir = -1;
					}
					
					createPedestreTimer -= createPedestreInterval;
					
					pos.y = pedestreMinY + mainState.linha1.y + Math.random() * (pedestreAreaHeight + mainState.linha3.y - (pedestreMinY + mainState.linha1.y));
					
					if (Math.random() < 0.1)
					{	
						var r:Rico = mainState.pessoasGroup.recycle(Rico) as Rico;
						r.respawn(mainState, pos, dir);
					}
					else
					{
						var po:Pobre = mainState.pessoasGroup.recycle(Pobre) as Pobre;
						po.respawn(mainState, pos, dir);
					}
				}
			}			
		}
		
	}

}